"""
Case Type   : SQL_LIMIT
Case Name   : 创建规则时，max_concurrency设置为其他字符，合理报错
Create At   : 2025/9/16
Owner       : lonely-dance
Description :
    1、修改参数;
    2、创建规则时，max_concurrency设置为其他字符
    3、清理环境
Expect      :
    1、修改成功
    2、创建失败
    3、清理环境成功
History     :
"""

import os
import time
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.ComThread import ComThread


class SqlLimit(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----- {os.path.basename(__file__)} start-----')
        self.comm_sh = CommonSH('PrimaryDbUser')
        self.pri_node = Node('PrimaryDbUser')
        self.standby_node = Node('Standby1DbUser')
        self.Constant = Constant()
        self.limit_name1 = 'limit0038_01'
        self.table_name = 'sql_limit0038'
        self.limit_id1 = 0
        self.thread_num = 3

    def test_main(self):
        text = '----step1:修改参数enable_sql_limit=on expect:成功----'
        self.log.info(text)
        self.comm_sh.execute_gsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'enable_sql_limit = on')
        self.comm_sh.execute_gsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'instr_unique_sql_count = 1000')

        text = '----step2:创建规则，max_concurrency设置为2 expect:成功----'
        self.log.info(text)
        cmd = f'''
        select gs_create_sql_limit('{self.limit_name1}','select',0,'aa','now()','2035-3-30 23:59:59','{{{{select,from}}}}','','');
        '''
        self.log.info(cmd)
        res = self.comm_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.assertIn('ERROR:  invalid input syntax for type bigint', res, '创建限流limit失败')

    def tearDown(self):
        text = '----step3:恢复环境 expect:成功----'
        self.log.info(text)
        self.comm_sh.execute_gsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'enable_sql_limit = off')
        self.comm_sh.execute_gsguc('reload', self.Constant.GSGUC_SUCCESS_MSG, 'instr_unique_sql_count = 100')

        self.log.info(f'----- {os.path.basename(__file__)} end-----')
